using voidbox;
using System;
using System.Numerics;
using TTEngine.Graphics;
using TTEngine.Resource;
using TTEngine.TTMath;

namespace TTEngine.Framework.UI
{


    //比例限制器
    public class UITargetFixer : UIBaseElement
    {
        public UITargetFixer()
        {
            this.LocalRect.SetAsFill();
        }
        public UITarget UITarget
        {
            get;
            set;
        }

        private float _autoSizeASPMax = 1.0f; // 最大宽高比，宽度除高度
        private float _autoSizeASPMin = 1.0f; // 最小宽高比
        public Material matDrawForeGround;
        public Material matDrawBackGround;
        public void SetAsp(float asp1, float asp2)
        {
            _autoSizeASPMin = MathF.Min(asp1, asp2);
            _autoSizeASPMax = MathF.Max(asp1, asp2);
        }

        fAABB BackScaleUV = fAABB.One;
        public override void OnUpdate(UICanvas canvas, float delta)
        {
            var sw = _parent.GetWorldRect();
            float aspNow = sw.Width / sw.Height;
            bool bFixMin = false;
            bool bFixMax = false;

            if (aspNow < _autoSizeASPMin) // 高度过大，缩高
            {
                aspNow = _autoSizeASPMin;
                bFixMin = true;
            }
            else if (aspNow > _autoSizeASPMax) // 宽度过大，缩宽
            {
                aspNow = _autoSizeASPMax;
                bFixMax = true;
            }
            if (UITarget == null)
                return;
            UITarget._parent = this;

            BackScaleUV = fAABB.One;
            // 是否高度调整
            if (bFixMin)
            {
                UITarget.LocalRect.SetHPosFill();
                float newHeight = MathF.Floor(sw.Width / aspNow);
                UITarget.LocalRect.SetVPosByCenter(newHeight);

                float scaleradio = 1.0f - (newHeight / sw.Height);
                BackScaleUV.x1 = scaleradio * 0.5f;
                BackScaleUV.x2 = 1.0f - scaleradio * 0.5f;
            }

            // 是否宽度调整
            if (bFixMax)
            {
                float newWidth = MathF.Floor(sw.Height * aspNow);
                UITarget.LocalRect.SetVPosFill();
                UITarget.LocalRect.SetHPosByCenter(newWidth);

                float scaleradio = 1.0f - (newWidth / sw.Width);
                BackScaleUV.y1 = scaleradio * 0.5f;
                BackScaleUV.y2 = 1.0f - scaleradio * 0.5f;
            }

            // 是否填充
            if (!bFixMax && !bFixMin)
            {
                UITarget.LocalRect.SetAsFill();
                BackScaleUV = fAABB.One;
            }
            UITarget.TargetUpdate(delta);

            base.OnUpdate(canvas, delta);
        }
        public override void OnRender(UICanvas canvas)
        {
            if (UITarget == null)
                return;
            UITarget.TargetRender();

            UITarget.RenderTarget.Linear = true;
            //恢复Canvas绘制状态
            canvas.Target.Apply();
            canvas._camera.Apply();
            var dest = UITarget.GetWorldRect();
            var destfull = this.GetWorldRect();
            canvas.Batcher.EndDraw();
            canvas.Batcher.ZTest = true;
            canvas.Batcher.ZWrite = true;

            if (matDrawForeGround != null)
            {
                matDrawForeGround._uniformTexs["tex"].Value = UITarget.Target as IRenderTarget;
                canvas.Batcher.GetSingleTextureBatcher().Draw(matDrawForeGround, fAABB.One, dest, 0, Color32.White);
            }
            else
            {
                canvas.Batcher.GetSingleTextureBatcher().Draw(UITarget.Target as IRenderTarget, fAABB.One, dest, 0, Color32.White);
            }
            //背景可以做个高斯模糊
            if (matDrawBackGround != null)
            {
                matDrawBackGround._uniformTexs["tex"].Value = UITarget.Target as IRenderTarget;
                canvas.Batcher.GetSingleTextureBatcher().Draw(matDrawBackGround, fAABB.One, dest, 0, Color32.White);
            }
            else
            {
                canvas.Batcher.GetSingleTextureBatcher().Draw(UITarget.Target as IRenderTarget, BackScaleUV, destfull, 0.1f, new Color(1, 1, 1, 0.25f));
            }
            canvas.Batcher.EndDraw();

            canvas.Batcher.ZTest = false;
            canvas.Batcher.ZWrite = false;


            base.OnRender(canvas);
        }
    }
}